package com.driving.single.common.redis.service.impl;

import com.driving.single.common.domain.TbPartner;
import com.driving.single.common.redis.service.CommonRedisPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CommonRedisPartnerServiceImpl implements CommonRedisPartnerService {
    @Value("${redis_key_frontend_partner}")
    private String redis_key_frontend_partner;   // 陪练KEY
    @Value("${redis_key_frontend_partner_expire_second}")
    private Integer redis_key_frontend_partner_expire_second;   // 陪练过期时间(单位：秒)

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 缓存教练
     * @param tbPartner 教练对象
     */
    @Override
    public Integer insertRedisTbPartner(TbPartner tbPartner) {
        String key = this.redis_key_frontend_partner + ":" + tbPartner.getId();
        this.redisTemplate.opsForValue().set(key,tbPartner,this.redis_key_frontend_partner_expire_second, TimeUnit.SECONDS);
        return 200;
    }
    /**
     * 查询缓存中的教练
     * @param id 主键
     */
    @Override
    public TbPartner selectRedisTbPartnerById(Long id) {
        String key = this.redis_key_frontend_partner + ":" + id;
        return (TbPartner) this.redisTemplate.opsForValue().get(key);
    }
}
